<template>
  <div>toRaw和markRaw</div>
  <h2>{{ state }}</h2>
  <button @click="testToRaw">测试toRaw</button>
  <button @click="testMarkRaw">测试markRaw</button>
</template>
<script lang="ts">
import { defineComponent, reactive, toRaw, markRaw } from 'vue'

interface UserInfo {
  name: string
  age: number
  likes?: string[]
}
export default defineComponent({
  name: 'App',
  setup() {
    const state = reactive<UserInfo>({
      name: '郝市民',
      age: 18
    })
    const testToRaw = () => {
      // 把代理对象变为普通对象了，数据变化，界面不变化
      const user = toRaw(state)
      user.name += '=='
      console.log(user.name);

    }
    const testMarkRaw = () => {
      const likes = ['吃', '喝']
      // markRaw标记的对象数据，从此以后都不能再成为代理对象了
      state.likes = markRaw(likes)
      setInterval(() => {
        if (state.likes) {
          state.likes[0] += '='
          console.log('定时器走起');
        }
      }, 1000)
    }
    return {
      state, testToRaw, testMarkRaw
    }
  }
})
</script>